﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string pageName = Request.Path;
                if (pageName.ToLowerInvariant().Contains("gamesbycategory.aspx"))
                {
                    GetCategoryList();
                    pnlCategory.Visible = true;
                    pnlMain.Visible = false;
                }
                else
                {
                    LoadNewGames();
                    LoadTopGames();
                    GetMemberActivities();
                    pnlCategory.Visible = false;
                    pnlMain.Visible = true;
                }

                CheckLogin();
            }
        }

        private void CheckLogin()
        {
            if (Request.Cookies.AllKeys.Contains("fguid"))
            {
                hlAccount.Visible = true;
                hlBookmarked.Visible = true;
                hlMessage.Visible = true;
                hlWalls.Visible = true;
                pnlLogin.Visible = false;
                pnlLogout.Visible = true;
                lblWelcome.Text = "Welcome, " + Request.Cookies["fguid"].Value.Split('/')[1];
            }
            else
            {
                hlAccount.Visible = false;
                hlBookmarked.Visible = false;
                hlMessage.Visible = false;
                hlWalls.Visible = false;
                pnlLogin.Visible = true;
                pnlLogout.Visible = false;
            }
        }

        private void GetMemberActivities()
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                GridActivities.DataSource = dataContext.GetMemberActivity();
                GridActivities.DataBind();
            }
        }

        private void GetCategoryList()
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                GridCategory.DataSource = dataContext.Categories;
                GridCategory.DataBind();
            }
        }

        private void LoadNewGames()
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                GridNewGames.DataSource = dataContext.GetNewGames(true);
                GridNewGames.DataBind();
            }
        }

        private void LoadTopGames()
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                GridTopGames.DataSource = dataContext.GetTopGames(true);
                GridTopGames.DataBind();
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Response.Cookies["fguid"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect("Default.aspx");
        }
    }
}